;(function(){

    class Banner{
        constructor(){
            this.bannerBtnL = document.getElementById("newgoods_main_btnl");        //轮播图左按钮
            this.bannerBtnR = document.getElementById("newgoods_main_btnr");        //轮播图右按钮
            this.bannerBox = document.querySelector("#newgoods_mainbox_m");          //轮播图图片盒子
            this.index = 0;
        }
        // ajax获取轮播图数据
        getdata(){
            var that = this ; 
            var p1 = pAjax({
                type:"post",
                url:"http://localhost/XMYP/src/php/bannerNew.php",
            });
            p1.then(function(res){
                that.data = JSON.parse(res);
                that.addElement();
            })
        }
        // 根据数据生成图片
        addElement(){
            var str = ` `;
            for(var i = 0 ; i < this.data.length ; i++){
                if(!this.data[i].moneyend){
                    this.data[i].moneyend = '';
                }
                str += ` <div class="newgoods_main_goods" goodsid=${this.data[i].goodsid} style="left:${i*270}px;">
                <div class="newgoods_maing_t"><img abc="${this.data[i].address}" class="lazy" alt=""></div>           
                <div class="newgoods_maing_b">
                    <p>${this.data[i].title}</p>
                    <p>${this.data[i].introduce}</p>
                    <p>
                        <span>￥</span>
                        <span>${this.data[i].money}</span>
                        <span>${this.data[i].moneyend}</span>
                    </p>
                </div>
            </div>`;
            }
            this.bannerBox.innerHTML = str;
            this.addEvent();
            this.init();
        }
        // 初始化
        init(){
            this.bannerBox.style.width = this.data.length*270+'px';
        }
        // 添加点击事件
        addEvent(){
            var that = this ;
            this.bannerBtnL.onclick = function(){
                that.move(1);
            }
            this.bannerBtnR.onclick = function(){
                that.move(2);
            }
            // 添加跳转事件
            for(let i = 0 ; i <  $(".newgoods_main_goods").length ; i++){
                $(".newgoods_main_goods").eq(i).on("click",()=>{
                    var str = $(".newgoods_main_goods").eq(i).attr("goodsid");
                    window.location.href = `http://localhost/XMYP/src/pages/details.html?goodsid=${str}`;
                })
            }
            // 懒加载
            var imgs = $(".lazy");
            var clientH = document.documentElement.clientHeight;
            lazyImg(imgs,clientH);
            onscroll = function(){
                lazyImg(imgs,clientH);
            }
           
        }
        move(i){
            if(i==1){
                if(this.index > 0 ){
                    this.index -= 1 ;
                    go(this.bannerBox,"left",-270*this.index);
                }
            }
            if(i==2){
                if(this.index < this.data.length-4){
                    this.index += 1 ;
                    go(this.bannerBox,"left",-270*this.index);
                }
            }
        }
    }

    let banner = new Banner();
    banner.getdata();



}());